<template>
	<base-page class="base-form" title="我的消息" @back="onCancel()">

		<view class="content" style="overflow-y: auto;">
			<!-- <u-sticky bgColor="#fff">
			<u-tabs :list="list" :is-scroll="false" style="" :current="current" @change="change"></u-tabs>
		</u-sticky> -->
			<view v-for="(item,index) in activityInfo" :key="index">
				<u-card :title="item.title" padding="30" margin="26rpx" :sub-title="item.startTime">
					<view class="content" slot="body">
						<view>
							<view class="u-body-item-title u-line-2" style="white-space: break-spaces">
								{{ item.content }}
							</view>
						</view>
						<view style="display: flex;justify-content: flex-end">
							<span style="font-size: 14px;" @click="downloadClick(item.url,item.className)"><uni-icons
									type="download" size="24">下载</uni-icons></span>
						</view>
					</view>
					<view slot="foot" style="display: flex;align-items: center;justify-content: space-between;">
						<view>班级：{{ item.className }}</view>
						<!-- <view>
						<button class="mini-btn" type="primary" size="mini" v-show="item.isAnnounce == 0"
							@click="publishActivity(item)">发布
						</button>
						<button v-if="item.isAnnounce == 0" class="mini-btn" type="warn" size="mini"
							@click="delActivity(item.id)">删除
						</button>
					</view> -->
					</view>
				</u-card>
			</view>
		</view>
	</base-page>
</template>

<script>
	import {
		CourseInfoController
	} from "@/api";
	import {
		getToken
	} from "@/utils";
	import projectConfig from '@/core/config'

	export default {
		data() {
			return {
				list: [{
					name: '全部通知'
				}, {
					name: '已发布'
				}, {
					name: '草稿',
				}],
				activityInfo: [],
				activityInfoAll: [],
				current: 0,
				swiperCurrent: 0,
				isAnnounce: null, //0 保存 ； 1 保存并发布
				jobDetailsArr: [], //作业详情
			}
		},
		mounted() {
			this.getActiveByTeacherIdFun();
		},
		methods: {
			
			buildGetUrl(actionName, params) {
				const queryString = this.objectToQueryString(params);
				if (actionName != null && actionName !== '') {
					if (actionName.substring(0, 1) === '/') actionName = actionName.substring(1);


					return (
						projectConfig.baseUrl + actionName + (queryString == null ? '' : '?' + queryString)
					);
				}
			},

			objectToQueryString(params) {
				if (params == null) {
					return null;
				} else {
					return Object.keys(params)
						.map(key => {
							if (params[key] !== undefined) {
								return `${key}=${params[key]}`;
							} else {
								return undefined;
							}
						})
						.filter(item => item != null)
						.join('&');
				}
			},
			getUploadFileUrl(item, params) {

				if (item == null || item.downloadUri == null) {
					return null;
				} else {

					const query = {};
					query.Authorization = getToken();
					query.filename = params

					return this.buildGetUrl(item.downloadUri, query);
				}
			},
			downloadClick(fileUrl, attachmentName) {
				var resObj = JSON.parse(fileUrl)[0];
				var src2 = this.getUploadFileUrl(resObj, resObj.filename);
				// console.log(src2)
				uni.downloadFile({
					//需要预览的文件地址
					url: src2,
					success: (res) => {
						if (res.statusCode === 200) {
							uni.hideLoading();
							//下载成功，得到文件临时地址
							console.log('下载成功1', res.tempFilePath);
							let newUrl = res.tempFilePath
							// 创建一个临时的 <a> 元素用于下载
							try {
								const link = document.createElement('a');
								link.href = newUrl;
								link.setAttribute('download', attachmentName + '.pdf');
								document.body.appendChild(link);
								link.click();
								document.body.removeChild(link);
								URL.revokeObjectURL(link.href);
							} catch (error) {
								uni.saveImageToPhotosAlbum({
									filePath: res.tempFilePath,
									success: function() {
										uni.showToast({
											title: "文件已保存到手机文件夹下",
											icon: "none"
										})
									}
								})
							}
							uni.showToast({
								title: '下载成功',
								icon: 'none',
							})
							uni.openDocument({
								filePath: res.tempFilePath,
								showMenu: true,
								success: function(file) {
									console.log('file-success', file);
								}
							});
						} else {
							uni.hideLoading();
							uni.showToast({
								title: '下载失败',
								icon: 'none',
							})
						}

					},
					fail() {
						uni.hideLoading();
						uni.showToast({
							title: '下载异常',
							icon: 'none',
						})
					}
				});


			},
			onCancel() {
				if (this.subPage) {
					this.$emit('close');
				} else {
					uni.navigateBack();
				}
			},
			renderTime(date) {
				var dateee = new Date(date).toJSON();
				return new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/,
					'')
			},
			publishActivity(row) {
				let classActivity = {
					id: row.id,
					isAnnounce: 1
				}
				updateInfo(classActivity).then((res) => {
					if (res.code == 200) {
						uni.showToast({
							title: `发布成功`
						})
						this.change(this.current)
					}
				}).catch((err) => {
					console.error(err);
				});
			},

			editActivity(row) {
				uni.navigateTo({
					url: '/pages/sendNotice/index?data=' + encodeURIComponent(JSON.stringify(row))
				})
			},
			delActivity(id) {
				let that = this;
				uni.showModal({
					title: '提示',
					content: '你确定要删除吗',
					success: function(res) {
						if (res.confirm) {
							delteClassActivityInfo(id).then((res) => {
								if (res.code == 200) {
									uni.showToast({
										title: `删除成功`
									})
									that.change(that.current)
								}
							}).catch((err) => {
								console.error(err);
							});
						} else if (res.cancel) {}
					},
				})
			},
			getActiveByTeacherIdFun() {
				CourseInfoController.getMyActivityList(this).then((res) => {
					this.activityInfo = res
				}).catch((err) => {
					console.error(err);
				});
			},
			change(index) {
				this.current = index;
				switch (index) {
					case 0:
						this.isAnnounce = null
						break;
					case 1:
						this.isAnnounce = 1
						break;
					case 2:
						this.isAnnounce = 0
						break;
				}
				this.activityInfo = [];
				for (var i = 0; i < this.activityInfoAll.length; i++) {

					if (this.isAnnounce == null) {
						this.activityInfo = this.activityInfoAll;
						break;
					}

					if (this.isAnnounce === 1 && this.activityInfoAll[i].isAnnounce === 1) {
						this.activityInfo.push(this.activityInfoAll[i])
						continue;
					}

					if (this.isAnnounce === 0 && this.activityInfoAll[i].isAnnounce === 0) {
						this.activityInfo.push(this.activityInfoAll[i])
					}
				}
			},
		},
		onLoad: function() {}
	}
</script>

<style scoped>
	page {
		height: 100%;
		background-color: #f2f2f2;
	}

	.is-disabled .uni-easyinput__placeholder-class[data-v-abe12412] {
		color: black !important;
		font-size: 14px !important;
	}

	.uni-easyinput>>>uni-textarea,
	.content>>>#unieasyinputA,
	#unieasyinputA {
		background-color: #ffffff !important;
	}

	#unieasyinputA>>>.uni-easyinput__content-textarea {
		margin: 0 !important;
	}

	#unieasyinputA>>>.is-input-border {
		border: none !important;
	}
</style>
<style scoped lang="scss">
	.u-card-wrap {
		background-color: $u-bg-color;
		padding: 1px;
	}

	.u-body-item {
		font-size: 32 rpx;
		color: #333;
		padding: 20 rpx 10 rpx;
	}

	.u-body-item image {
		width: 120 rpx;
		flex: 0 0 120 rpx;
		height: 120 rpx;
		border-radius: 8 rpx;
		margin-left: 12 rpx;
	}

	.u-line-2,
	.u-line-3,
	.u-line-4,
	.u-line-5 {
		display: unset !important;
	}
</style>